/*==================================================
project:       Cleans up SISBEN for use
Author:        David L. Vargas 
E-email:       davidvar@iadb.org
url:           
Dependencies:  
----------------------------------------------------
Creation Date:    May 2023
Modification Date:   
Do-file version:    01
References:          
Output:             
==================================================*/


/*==================================================
            0: Program set up
==================================================*/
*Written on STATA 17
drop _all
set varabbrev off	// no variable abbreviations allowed (personal preference)

** source dir
cd "${dir5r}" // graph dir

/*==================================================
            1: load and transformations
==================================================*/

*----------- 1.0.1 Load CRRA welfare FUN
do "${dir6r}/ados/bstrap_pmt.ado" // load bstap FUN

*----------  1.1. Data prep:
use "${dir3r}/01_survey/survey_targeting_r1_long.dta", clear

*----------  1.1.2 Estimate PMT for survey data:

*Globals with updted survey variables
glo hh_char_srv urban age_feb20_srv prop_kids_srv n_members_srv elementary_srv highschool_srv tertiary_edu_srv children_srv living_couple
glo assets_srv own_washing_machine_srv own_tractor_srv own_moto_srv own_car_srv own_PC_srv own_fridge_srv
glo hh_srv  wall_finished_srv floor_finished_srv cookpower_connected_srv wc_flush_srv kitchen_srv electricity_srv running_water_srv trash_service_srv 
glo labour_srv formal_work_srv informal_work_srv

glo pmt_1_srv  $hh_char_srv $assets_srv $hh_srv $labour_srv

reg l_pp_inc_srv $pmt_1_srv [aw=pondera] if year==2019
predict  x if  year==2019
  

egen pred_income01_srv = mean(x), by(id_hogar_SISBEN)
drop x 

*------------ poverty line
scalar epovline = 137350
scalar l_epovline = log(epovline)

scalar povline = 327674
scalar l_povline = log(povline)

*----------- dummys of category 

* predicted extr. poor
gen pred_epoor = pred_income01_srv <= l_epovline
replace pred_epoor = . if pred_income01_srv == .

* predicted poor
gen pred_poor = pred_income01_srv <= l_povline & pred_income01_srv > l_epovline
replace pred_poor = . if pred_income01_srv == .

* predicted non-poor
gen pred_nonpoor = pred_income01_srv > l_povline
replace pred_nonpoor = . if pred_income01_srv == .

* Actual extr. poor
gen targeted = l_pp_inc_srv < l_epovline
replace targeted = . if l_pp_inc_srv == .

g work_b=work_p if year==2019
egen aux1=max(work_b), by(id_vivienda_SISBEN)

g work_post=work_p if year==2020
egen aux2=max(work_post), by(id_vivienda_SISBEN)

g job_loss1y=aux2==0 & aux1==1

collapse (mean) targeted [aw=pondera], by(year pred_nonpoor pred_poor pred_epoor job_loss1y)

/*==================================================
        2: Create figure
==================================================*/

#delimit ;
tw      (connected targeted year if pred_epoor == 1 & job_loss1y==1,lcolor(blue)  mcolor(blue) msize(large))
(connected targeted year if pred_epoor == 1 & job_loss1y==0 , lcolor(blue) lpattern(dash) mcolor(blue)) 
(connected targeted year if pred_poor == 1 & job_loss1y==1, lcolor(red)  mcolor(red) msize(large))
(connected targeted year if pred_poor == 1 & job_loss1y==0 , lcolor(red) lpattern(dash) mcolor(red))
(connected targeted year if pred_nonpoor == 1 & job_loss1y==1 , lcolor(black)  mcolor(black) msize(large)) 
(connected targeted year if pred_nonpoor == 1 & job_loss1y==0 , lcolor(black) lpattern(dash) mcolor(black)) 


,
legend(order(1 "Predicted extreme poor (Job loss)"
                2 "Predicted extreme poor (No job loss)"
                3 "Predicted poor (Job loss)" 
                4 "Predicted poor (No job loss)" 
                5 "Predicted non poor (Job loss)"
                6 "Predicted non poor (No job loss)")
        pos(6) col(2))
ytitle("Prob. of Extreme Poverty")
xtitle("Year")
xlab(2019(1)2021)	
;
#delimit cr

graph export "F4_pr_epov.png", as(png) replace
graph export "F4_pr_epov.pdf", as(pdf) replace




